package com.one.utils;

import com.one.dto.*;
import com.one.entity.UserMenuEntity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class TreeUtils {
    public static List createDeptTree(List<SysDept> data, int parent) {
        if(data.size()==0){
            return null;
        }
        List<SysDept> children = new ArrayList<>();
        for(SysDept tree:data){
            if(tree.getParentId()==parent){
                List<SysDept> children1 = new ArrayList<>(data);
                children1.remove(tree);
                tree.setChildren(createDeptTree(children1,tree.getDeptId()));
                children.add(tree);
            }
        }
        return children;
    }

    public static List createOperateTree(List<SysOperate> data, String menuId) {
        if(data.size()==0){
            return null;
        }
        List<SysOperate> children = new ArrayList<>();
        for(SysOperate tree:data){
            if(tree.getMenuId().equals(menuId)){
                List<SysOperate> childrenData = new ArrayList<>(data);
                childrenData.remove(tree);
                tree.setChildren(createOperateTree(childrenData,tree.getId()));
                children.add(tree);
            }
        }
        return children;
    }

    public static List createMenuTree(List<SysMenu> data, String menuId) {
        if(data.size()==0){
            return null;
        }
        List<SysMenu> children = new ArrayList<>();
        for(SysMenu tree:data){
            if(tree.getParentId().equals(menuId)){
                List<SysMenu> childrenData = new ArrayList<>(data);
                childrenData.remove(tree);
                tree.setChildren(createMenuTree(childrenData,tree.getId()));
                children.add(tree);
            }
        }
        return children;
    }

    public static List getUserMenuTree(List<UserMenuEntity> data, String menuId) {
        if(data.size()==0){
            return null;
        }
        List<UserMenu> children = new ArrayList<>();
        for(UserMenuEntity tree:data){
            if(tree.getParentId().equals(menuId)){
                List<UserMenuEntity> childrenData = new ArrayList<>(data);
                childrenData.remove(tree);
                UserMenu userMenu = new UserMenu();
                userMenu.setPath(tree.getPath());
                userMenu.setId(tree.getMenuId());
                userMenu.setMeta(new Meta(tree.getName(),tree.getIcon()));
                userMenu.setChildren(getUserMenuTree(childrenData,tree.getMenuId()));
                children.add(userMenu);
            }
        }
        return children;
    }

    public static List createIndicatorTree(List<IndicatorInfo> data, String indicatorId,int score) {
        if(data.size()==0){
            return null;
        }
        List<IndicatorInfo> children = new ArrayList<>();
        for(IndicatorInfo tree:data){
            if(tree.getParentId().equals(indicatorId)){
                List<IndicatorInfo> childrenData = new ArrayList<>(data);
                childrenData.remove(tree);
                tree.setScore((int)(score*tree.getWeight()));
                tree.setChildren(createIndicatorTree(childrenData,tree.getId(),tree.getScore()));
                children.add(tree);
            }
        }
        return children;
    }
}
